<?php
namespace app\Manage\controller;
use app\common\controller\Manage;
use app\common\model\User as UserModel;
use think\facade\Request;

class User extends Manage
{
    /**
     * 用户列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if(Request::isAjax())
        {
            $userModel = new UserModel();
            return $userModel->tableData(input('param.'));
        }
        else
        {
            return $this->fetch('index');
        }
    }


    /**
     * 添加用户
     * @return array|mixed
     */
    public function addUser()
    {
        $this->view->engine->layout(false);
        if(Request::isPost())
        {
            $input = Request::param();
            $userModel = new UserModel();
            $result = $userModel->manageAdd($input);
            return $result;
        }
        return $this->fetch('addUser');
    }


    /**
     * 编辑用户
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editUser()
    {
        $this->view->engine->layout(false);
        $userModel = new UserModel();

        if(Request::isPost())
        {
            $input = Request::param();
            $result = $userModel->manageEdit($input);
            return $result;
        }

        $user_id = Request::param('user_id');
        $info = $userModel->getUserInfo($user_id);
        $this->assign('info', $info);
        return $this->fetch('editUser');
    }


    /**
     * 用户详情
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function details()
    {
        $this->view->engine->layout(false);

        $user_id = Request::param('user_id');
        $model = new UserModel();
        $info = $model->getUserInfo($user_id);
        $this->assign('info', $info);
        //echo json_encode($info);
        return $this->fetch('details');
    }
}